/**
 * 
 */
package com.rangers.manage.sms.domain;

import java.util.Base64;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import lombok.Data;

/**
 * 云讯供应商配置
 * 
 * {@link http://www.ytx.net/new_File/FileDailBackCall.aspx}
 * 
 * @author fuhw
 * @date 2017年11月20日
 */
@Data
public class YxProviderConfig {

	public static String DEFAULT_VERSION = "201512";

	public static String FUNC_CALL = "call";// 语音类func
	public static String FUNC_SMS = "sms";// 语音类func

	public static String FUNCURL_NOTICEALL = "NoticeCall.wx";// 语音通知funcUrl
	public static String FUNCURL_DAILBACK = "DailbackCall.wx";// 双向通话funcUrl
	public static String FUNCURL_TEMPLATESMS = "TemplateSMS.wx";// 短信服务funcUrl

	public static String ACTION_NOTIFY_CALL = "templateNoticeCall";// 语音通知
	public static String ACTION_DAIL_BACK = "callDailBack";// 双向通话
	public static String ACTION_TEM_SMS = "templateSms";// 短信服务

	private String accountSID;
	private String authToken;
	private String version = DEFAULT_VERSION; // 目前可选版本：201512

	public YxProviderConfig(String accountSID, String authToken) {
		super();
		this.accountSID = accountSID;
		this.authToken = authToken;
	}

	public YxProviderConfig(String accountSID, String authToken, String version) {
		super();
		this.accountSID = accountSID;
		this.authToken = authToken;
		this.version = version;
	}

	private String curTimestamp;
	public static String VOICE_PHONE_TIMESTAME_PATTERN = "yyyyMMddHHmmss";

	// 包头验证信息：base64加密(账户Id + "|" + 时间戳) 说明：时间戳有效时间为24小时
	// 格式"yyyyMMddHHmmss"，如：20140416142030
	public String getAuthorization() {
		Assert.hasText(this.accountSID, "this accoutSID must be not null");
		StringBuffer sb = new StringBuffer();
		if (!StringUtils.hasText(this.curTimestamp)) {
			this.curTimestamp = DateFormatUtils.format(new Date(), VOICE_PHONE_TIMESTAME_PATTERN);
		}
		sb.append(this.accountSID).append("|").append(this.curTimestamp);
		byte[] encode = Base64.getEncoder().encode(sb.toString().getBytes());
		return new String(encode);
	}

	/**
	 * API 验证参数 ：MD5加密（账户Id + 账户授权令牌 +时间戳)
	 * *URL后必须带有Sign参数，例如：Sign=AAABBBCCCDDDEEEFFFGGG *时间戳需与Authorization中时间戳相同
	 * 注:MD5加密32位,无论大小写
	 */
	public String getSign() {
		Assert.hasText(this.accountSID, "this accoutSID must be not null");
		Assert.hasText(this.authToken, "this authToken must be not null");
		if (!StringUtils.hasText(this.curTimestamp)) {
			this.curTimestamp = DateFormatUtils.format(new Date(), VOICE_PHONE_TIMESTAME_PATTERN);
		}
		StringBuffer sb = new StringBuffer();
		sb.append(this.accountSID).append(this.authToken).append(this.curTimestamp);
		return DigestUtils.md5DigestAsHex(sb.toString().getBytes());
	}

	// public static void main(String[] args) {
	// VoicePhoneProviderConfig vp = new
	// VoicePhoneProviderConfig("a15aaf5ddd434a4396cf90ae478e0f2e",
	// "b1d5809e9b1c4e129bcdad4d9e120e9c", "201512", "call", "NoticeCall.wx");
	// vp.setCurTimestamp("20171111134901");
	// System.err.println(vp.getAuthorization().equals("YTE1YWFmNWRkZDQzNGE0Mzk2Y2Y5MGFlNDc4ZTBmMmV8MjAxNzExMTExMzQ5MDE="));
	// System.err.println(vp.getSign().equals("ae7596192f02d422d280845a063e7013"));
	// }

}
